using Microsoft.AspNetCore.Mvc;
using MallSystembackend.Api.Model;
using MallSystembackend.Api.Repository;
using Microsoft.Extensions.Configuration;
using MallSystembackend.Api.ParamModel;
using System.Linq;
namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class EvaluateController : ControllerBase
    {
        public IRepository<Evaluate> _evaluateRepository;

        public readonly IConfiguration _configuration;

        public EvaluateController(IRepository<Evaluate> evaluateRepository,
        IConfiguration configuration)
        {
            _configuration = configuration;
            _evaluateRepository = evaluateRepository;
        }

        //添加评价
        [HttpPost]
        public ActionResult AddEvaluate(ParamEvaluate model)
        {
            var addEvaluate = new Evaluate
            {
                CommodityId = model.CommodityId,
                UsersId = model.UsersId,
                EvaluateContent = model.EvaluateContent,
                EvaluateScore = model.EvaluateScore
            };
            _evaluateRepository.Insert(addEvaluate);
            return Ok(new
            {
                Code = 1000,
                Data = addEvaluate,
                Msg = "添加成功"
            });
        }
        //根据商品id获得评价
        [HttpGet("{id}")]
        public ActionResult GetEvaluates(int id)
        {
            var GetEvaluate = _evaluateRepository.Table;
            var getevaluates = GetEvaluate.Where(x => x.CommodityId == id).Where(x=>x.IsDeleted==false);
            return Ok(new
            {
                Code = 1000,
                Data = getevaluates,
                Msg = "获得成功"
            });
        }
    }
}